package com.itheima.store.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.itheima.store.domain.Book;
import com.itheima.store.domain.BookCategory;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.User;
import com.itheima.store.service.BookCategoryService;
import com.itheima.store.service.BookService;
import com.itheima.store.service.impl.BookCategoryServiceImpl;
import com.itheima.store.utils.BaseServlet;
import com.itheima.store.utils.BeanFactory;
import com.itheima.store.utils.UUIDUtils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

/**
 * Servlet implementation class AdminCategoryServlet
 * 后台分类管理的
 */

public class AdminBookCategoryServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	public String findAll(HttpServletRequest request,HttpServletResponse response) {
		BookCategoryService categoryService =(BookCategoryService)BeanFactory.getBean("bookcategoryService");
		PageBean<BookCategory> pageBean;
		try {
			//清空缓存
			CacheManager cacheManager = CacheManager.create(BookCategoryServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml"));
			// 从配置文件中获取名称为categoryCache缓存区
			Cache cache = cacheManager.getCache("bookcategorycache");
			cache.remove("list");
			
			//获取参数
			Integer currPage = Integer.parseInt(request.getParameter("currPage"));
			pageBean = categoryService.findAllByPage(currPage);
			request.setAttribute("pageBean", pageBean);
			//List<BookCategory> categoryList = categoryService.findAll();
			//request.setAttribute("list", categoryList);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/admin/category/list.jsp";
	}
	//跳转到添加分类的页面
	public String saveUI(HttpServletRequest request,HttpServletResponse response) {
		
		return "/admin/category/add.jsp";
		
	}
	//保存分类到数据库表
	public String save(HttpServletRequest request,HttpServletResponse response) throws IOException {
		User existUserAdmin =(User) request.getSession().getAttribute("existUserAdmin");
		String user_id=existUserAdmin.getUser_id();
		String user_name = existUserAdmin.getUser_name();
		String cname  = request.getParameter("cname");
		BookCategoryService categoryService =(BookCategoryService)BeanFactory.getBean("bookcategoryService");
		String cid = UUIDUtils.getUUID();
        Date date=new Date();                             
        SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String date1=temp.format(date);  
        Date date2 = null;
		try {
			date2 = temp.parse(date1);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		BookCategory category = new BookCategory();
		category.setCategory_id(cid);
		category.setCategory_name(cname);
		category.setCreateTime(date2);
		category.setCreatePerson_id(user_id);
		category.setCreatePerson(user_name);
		try {
			categoryService.save(category);
			response.sendRedirect(request.getContextPath()+"/AdminBookCategoryServlet?method=findAll&currPage=1");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	//编辑修改页面
	public String editUI(HttpServletRequest request,HttpServletResponse response) {
		
		BookCategory category;
		try {
			//获取参数
			String cid = request.getParameter("cid");
			BookCategoryService categoryService = (BookCategoryService)BeanFactory.getBean("bookcategoryService");
			category = categoryService.findByCid(cid);
			if(category !=null) {
				request.setAttribute("category", category);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return "/admin/category/edit.jsp";
	}
	
	//更新Category表
	public String update(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		Map<String, String[]> parameterMap = request.getParameterMap();
		BookCategory category = new BookCategory();
		BeanUtils.populate(category, parameterMap);
		BookCategoryService categoryService = (BookCategoryService)BeanFactory.getBean("bookcategoryService");
		try {
			categoryService.update(category);
			response.sendRedirect(request.getContextPath()+"/AdminBookCategoryServlet?method=findAll&currPage=1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	//删除分类
	public String deleteCategory(HttpServletRequest request,HttpServletResponse response) {
		try {
		String cid  = request.getParameter("cid");
		BookCategoryService categoryService = (BookCategoryService)BeanFactory.getBean("bookcategoryService");
		categoryService.deleteCategoryByCid(cid);
		response.sendRedirect(request.getContextPath()+"/AdminBookCategoryServlet?method=findAll&currPage=1");
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
}
